package com.hec.yygh.user.controller;


import com.hec.yygh.common.result.R;
import com.hec.yygh.common.utils.AuthContextHolder;
import com.hec.yygh.common.utils.JwtHelper;
import com.hec.yygh.model.user.UserInfo;
import com.hec.yygh.user.service.UserInfoService;
import com.hec.yygh.vo.user.LoginVo;
import com.hec.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author hec
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/api/userinfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 登入
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        Map<String,Object> map = userInfoService.login(loginVo);
        return R.ok().data(map);
    }

    @GetMapping("/auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return R.ok().data("userInfo",userInfo);
    }

    /**
     * 保存用户认证信息
     * @param userAuthVo
     * @return
     */
    @PostMapping("/auth/userAuth")
    public R saveUserAuth(@RequestBody UserAuthVo userAuthVo,HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        userInfoService.saveUserAuth(userAuthVo,userId);
        return R.ok();
    }

}

